package com.pengweiwei.androidzygote.start;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.viewpager.widget.ViewPager;

import com.putong35.common.baseactfrag.activity.BaseActivity;
import com.putong35.common.customviews.multiviewpager.MultiViewPager;
import com.pengweiwei.androidzygote.R;

import butterknife.BindView;


public class ImageGuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private int images[] = new int[]{
            R.drawable.image_guide_1,
            R.drawable.image_guide_2,
            R.drawable.image_guide_3,
            R.drawable.image_guide_4};
    private int lastPage = images.length - 1;

    @BindView(R.id.mvpGuide)
    public MultiViewPager mvpGuide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
        mvpGuide = (MultiViewPager) findViewById(R.id.mvpGuide);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
//        //定义全屏参数
//        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
//        //获得窗口对象
//        Window mywindow = this.getWindow();
//        //设置Flag标识
//        mywindow.setFlags(flag,flag);
        mvpGuide.setFragmentManager(getSupportFragmentManager());
        initGuideFragment();
    }

    ImageGuideFragment[] fragments;
    private void initGuideFragment() {
        ImageGuideFragment guideOneFragment = ImageGuideFragment.newInstance(images[0]);
        ImageGuideFragment guideTwoFragment = ImageGuideFragment.newInstance(images[1]);
        ImageGuideFragment guideThreeFragment = ImageGuideFragment.newInstance(images[2]);
        ImageGuideFragment guideFourFragment = ImageGuideFragment.newInstance(images[3], true);
        fragments = new ImageGuideFragment[]{guideOneFragment,guideTwoFragment,guideThreeFragment,guideFourFragment};

        for (int i = 0; i < fragments.length; i++) {
            mvpGuide.add(fragments[i]);
        }
        mvpGuide.addOnPageChangeListener(this);
        mvpGuide.setOffscreenPageLimit(2);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            fragments[0].onPageSelected();
        }
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_guide,null);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        fragments[position].onPageSelected();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    public interface PageSelectedListener {
        void onPageSelected();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }
}
